package ReadWriteXML;
import java.util.ArrayList;

public class CQuestion {
	private int _idQuestion;
	private int _type;
	private int _length;
	private String _content;
	private int _time;
	//private String _trueFalse;
	private ArrayList<COption> _listOption;
	
	public CQuestion()
	{
		_type = 0;
		_length = 0;
		_content = "";
		_time = 0;
		//_trueFalse = "";
		_listOption = new ArrayList<COption>();
	}
	
	public CQuestion(int type, int length, String content, int time, ArrayList<COption> listOption)
	{
		_type = type;
		_length = length;
		_content = content;
		_time = time;
		_listOption = new ArrayList<COption>(listOption);
		
		
	}
	//properties
	public void set_type(int _type) {
		this._type = _type;
	}
	public int get_type() {
		return _type;
	}
	public void set_length(int _length) {
		this._length = _length;
	}
	public int get_length() {
		return _length;
	}
	public void set_content(String _content) {
		this._content = _content;
	}
	public String get_content() {
		return _content;
	}
	/*public void set_trueFalse(String _trueFalse) {
		this._trueFalse = _trueFalse;
	}
	public String get_trueFalse() {
		return _trueFalse;
	}*/
	public void set_listOption(ArrayList<COption> _listOption) {
		this._listOption = _listOption;
	}
	public ArrayList<COption> get_listOption() {
		return _listOption;
	}
	
	public void addOption(COption option)
	{
		_listOption.add(option);
		
	}
	public void set_time(int _time) {
		this._time = _time;
	}
	public int get_time() {
		return _time;
	}

	public void set_idQuestion(int _idQuestion) {
		this._idQuestion = _idQuestion;
	}

	public int get_idQuestion() {
		return _idQuestion;
	}
	
	/**
	 * Hàm trả về một CQuestion dựa vào một mảng chuỗi text
	 * @param sArr
	 * @return
	 */
	public static CQuestion getQuestion(String[] sArr)
	{
		CQuestion quest = new CQuestion();
		quest.set_idQuestion(Integer.valueOf(sArr[0]));
		quest.set_type(Integer.valueOf(sArr[1]));
		quest.set_content(sArr[2]);
		switch(quest.get_type())
		{
		case 1://câu hỏi single choice
		{
			for(int i=3; i<sArr.length; i+=2)
			{
				COption opt = new COption();
				opt.set_idDetail(Integer.valueOf(sArr[i]));
				opt.set_content(sArr[i+1]);
				quest.addOption(opt);
			}
			break;
		}
		case 2://câu hỏi multi choice
		{
			for(int i=3; i<sArr.length; i+=2)
			{
				COption opt = new COption();
				opt.set_idDetail(Integer.valueOf(sArr[i]));
				opt.set_content(sArr[i+1]);
				quest.addOption(opt);
			}
			break;
		}
		case 4:
		{
			int temp = 1;
			int key;
			for(int i=3; i<sArr.length; i+=2)
			{
				if(temp > 0)
					key = 1;
				else 
					key = 2;
				COption opt = new COption();
				opt.set_idDetail(Integer.valueOf(sArr[i]));
				opt.set_value(sArr[i+1]);
				opt.set_key(""+key);
				quest.addOption(opt);
			}
			break;
		}
		}
		quest.set_length(quest.get_listOption().size());
		return quest;
	}
	
	
}
